<?php
/**
 * created by zzy
 * date: 2017/11/16 9:55
 */

namespace App\Eloquent\Ygt;

use Illuminate\Database\Eloquent\SoftDeletes;

class Company extends DbEloquent{

    use SoftDeletes;
    protected $table    = 'ygt_company';

    //添加或者修改一条数据
    public static function edit($data,$pk='')
    {
        $id             = isset($data['id']) ? $data['id'] : 0;
        $scanCode       = isset($data['scan_code']) ? $data['scan_code'] : 1;
        $data['logo']       = isset($data['logo']) ? $data['logo'] : 0;
        $data['business_licence']       = isset($data['business_licence']) ? $data['business_licence'] : 0;
        $data['industry_license']       = isset($data['industry_license']) ? $data['industry_license'] : 0;
        $data['other_license']       = isset($data['other_license']) ? $data['other_license'] : 0;
        $title          = $data['title'];
        $pinyin         = app('pinyin')->abbr($title);
        $firstLetter    = strtoupper(substr($pinyin,0,1));

        //省市区信息
        $provinceInfo = \App\Eloquent\Province::where('provinceID',$data['province_id'])->first();
        $cityInfo = \App\Eloquent\City::where('cityID',$data['city_id'])->first();
        $areaInfo =\App\Eloquent\ Area::where('areaID',$data['area_id'])->first();

        $provinceInfo2 = \App\Eloquent\Province::where('provinceID',$data['invoice_province_id'])->first();
        $cityInfo2 = \App\Eloquent\City::where('cityID',$data['invoice_city_id'])->first();
        $areaInfo2 =\App\Eloquent\ Area::where('areaID',$data['invoice_area_id'])->first();



        $companyData    = [
            'title'=>$title,
            'scan_code'=>$scanCode,
            'first_letter'=>$firstLetter,
            'logo'=>$data['logo'],
            'business_licence'=>$data['business_licence'],
            'industry_license'=>$data['industry_license'],
            'other_license'=>$data['other_license'],
            'contacts_name'=>$data['contacts_name'],
            'mobile'=>$data['mobile'],
            'consignee_name'=>$data['consignee_name'],
            'consignee_tel'=>$data['consignee_tel'],
            'province_id'=>$data['province_id'],
            'city_id'=>$data['city_id'],
            'area_id'=>$data['area_id'],
            'address'=>$data['address'],
            'province_name' => isset($provinceInfo->province)?$provinceInfo->province:'',
            'city_name' => isset($cityInfo->city)?$cityInfo->city:'',
            'area_name' => isset($areaInfo->area)?$areaInfo->area:'',

            //开票资料信息
            'invoice_account_name'=>$data['invoice_account_name'],
            'invoice_taxpayer_number'=>$data['invoice_taxpayer_number'],
            'invoice_telphone'=>$data['invoice_telphone'],
            'invoice_opening_bank'=>$data['invoice_opening_bank'],
            'invoice_bank_card_number'=>$data['invoice_bank_card_number'],
            'invoice_province_id'=>$data['invoice_province_id'],
            'invoice_city_id'=>$data['invoice_city_id'],
            'invoice_area_id'=>$data['invoice_area_id'],
            'invoice_address'=>$data['invoice_address'],
            'invoice_province_name' => isset($provinceInfo2->province)?$provinceInfo2->province:'',
            'invoice_city_name' => isset($cityInfo2->city)?$cityInfo2->city:'',
            'invoice_area_name' => isset($areaInfo2->area)?$areaInfo2->area:'',

        ];
        if( $id>0){
            $where          = ['id'=>$id];
            $result         = self::updateOneData($where, $companyData,$pk);
        } else {
            $result         = self::insertOneData($companyData,$pk);
        }
        return $result;
    }
    //领取材料时是否需要扫描
    //true需要false不需要
    public static function isScanCode($id)
    {
        $scanCode           = self::getOneValueById($id,'scan_code');
        $result             = $scanCode==1 ? true : false;
        return $result;
    }

    //根据type不同联合的表不同
    //默认不联合表
    public static function getJoinList($where,$limit='',$offset='',$type=1,$orderby=['ygt_company.id','desc'])
    {
        switch($type)
        {
            case 1:
                //取材料的流水列表
                $join = [
                    ['table'=>'ygt_seller_company','field_l'=>'ygt_seller_company.company_id','field_c'=>'=','field_r'=>'ygt_company.id'],
                ];
                $column             = 'ygt_company.id,ygt_company.title,ygt_company.first_letter';
                break;
            default:
                $column             = 'ygt_company.*';
                $join               = '';
        }
        $groupby            = '';
        $result             = self::getList($where,$column,$limit,$offset,$orderby,$groupby,$join);
        return $result;
    }
}